/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.buzzwords.behaviors;

import com.buzzwords.model.Buzzword;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jdo.Query;

/**
 *
 * @author devinpartlow
 */
public class BuzzwordCustomBehavior extends BuzzwordBehavior {

    @Override
    public List<Object> read(Object value, List<String> properties) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Object> excute(String filter, String declareParameters,
            Map<String, Object> parameters, List<String> properties) {

        Query query = pm.newQuery(Buzzword.class);
        query.setFilter(filter);
        query.declareParameters(declareParameters);

        // get results
        try {
            List<Object> objects = (List<Object>) query.executeWithMap(parameters);
            List<Object> objectsToDetach = new ArrayList<Object>(objects);
            for (Object object : objects) {
                Buzzword buzzword = (Buzzword) object;

                if (properties.contains(Buzzword.BUZZWORD_LOCALES)) {
                        buzzword.getLocales();
                        objectsToDetach.add(buzzword);
                }
                continue;
            }
            pm.detachCopyAll(objectsToDetach);
            return objects;
        } finally {
            query.closeAll();
        }
    }
}
